/*
 * MIT License
 *
 * Copyright (c) 2024-2048 冰羽
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package cn.star.framework.extend.config;

import cn.star.framework.extend.provider.DruidConnectionProvider;
import cn.star.framework.extend.provider.HikariConnectionProvider;
import cn.star.framework.extend.type.DatabaseType;
import cn.star.framework.extend.type.PoolType;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.sql.DataSource;
import lombok.Getter;
import lombok.Setter;

/**
 * Cfg<br>
 *
 * @author zhaoweiping
 *     <p style='color: red'>Created on 2024-09-23 11:06:00
 * @since 3.0.0
 */
public class Cfg {

  /** {@link DatabaseType} */
  @Getter @Setter private String type;

  @Getter @Setter private String catalog;
  @Getter @Setter private String schema;

  @Getter @Setter private String url;
  @Getter @Setter private String username;
  @Getter @Setter private String password;
  @Getter @Setter private String driver;
  @Getter @Setter private String dialect;

  @Getter @Setter private PoolType poolType;
  @Getter @Setter private int poolSize = 10;
  @Getter @Setter private String testQuery;

  // Spring、Hibernate

  @Getter @Setter private DataSource dataSource;


  /** {@link AvailableSettings} */
  private final Map<String, Object> map = new HashMap<>();

  public Cfg() {}

  public Cfg addProperty(String key, Object value) {
    this.map.put(key, value);

    return this;
  }

  public Object getProperty(String key) {
    return this.map.get(key);
  }

  public void wrapper(Properties properties, Object key, Object value) {
    try {
      properties.put(key, value);
    } catch (Exception ignore) {
    }
  }

  public Properties unwrap() {
    Properties properties = new Properties();
    this.map.forEach((k, v) -> this.wrapper(properties, k, v));

    // define merge、cover

    this.wrapper(properties, AvailableSettings.URL, url);
    this.wrapper(properties, AvailableSettings.USER, username);
    this.wrapper(properties, AvailableSettings.PASS, password);
    this.wrapper(properties, AvailableSettings.DRIVER, driver);
    this.wrapper(properties, AvailableSettings.DIALECT, dialect);

    Object connectionProvider = properties.get(AvailableSettings.CONNECTION_PROVIDER);

    if (connectionProvider == null) {
      if (poolType == PoolType.HIKARI) {
        this.wrapper(
            properties, AvailableSettings.CONNECTION_PROVIDER, HikariConnectionProvider.class);
      } else if (poolType == PoolType.DRUID) {
        this.wrapper(
            properties, AvailableSettings.CONNECTION_PROVIDER, DruidConnectionProvider.class);
      } else {
        this.wrapper(
            properties, AvailableSettings.CONNECTION_PROVIDER, HikariConnectionProvider.class);
      }
    }

    this.wrapper(properties, AvailableSettings.POOL_SIZE, poolSize);
    this.wrapper(properties, AvailableSettings.DYNAMIC_CFG, this);

    // SessionFactory#getCurrentSession值为null的问题
    // jta/thread
    // org.hibernate.internal.SessionFactoryImpl#CURRENT_SESSION_CONTEXT_CLASS
    this.wrapper(properties, AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, "thread");

    return properties;
  }
}
